package com.example.esaclient;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.webkit.URLUtil;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SettingsActivity extends Activity {

	private static SharedPreferences mSharedPreferences;
	// Preference Constants
	static final String PREF_ESA_URL = "esa_url";
	String proxy_server_url="";
	
	private EditText txtUrl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		
		txtUrl = (EditText) findViewById(R.id.txtUrl);
		
		//checking already saved url
		mSharedPreferences = getApplicationContext().getSharedPreferences("MyPref", 0);
		proxy_server_url = mSharedPreferences.getString(PREF_ESA_URL, "");
		
		if (!proxy_server_url.trim().equals("")) {
			txtUrl.setText(proxy_server_url);
		}
		
	}

	
	public void saveSettings(View v){
		TextView tvError= (TextView)findViewById(R.id.txtError);
		proxy_server_url=txtUrl.getText().toString();
		if (Patterns.WEB_URL.matcher("http://"+proxy_server_url).matches()){
		Editor e = mSharedPreferences.edit();
		e.putString(PREF_ESA_URL, proxy_server_url);
		e.commit();
		tvError.setVisibility(View.GONE);
		Toast toast = Toast.makeText( getApplicationContext(), "Settings saved!",  Toast.LENGTH_SHORT);
		toast.show();
		}else
		tvError.setVisibility(View.VISIBLE);
	}
	
}
